package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class WorkTypeSelectWindow extends BasePopWindow {

    private TextView okView;
    private TextView cancelView;
    private MaxRecyclerView rvWorkType;
    private PickAdapter workTypeAdapter;

    private List<PickSelectEntity> pickSelectEntityList;

    private String name = "";

    public WorkTypeSelectWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.laout_work_type_window;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        pickSelectEntityList = new ArrayList<>();

        okView = holder.getPop().findViewById(R.id.ok_view);
        cancelView = holder.getPop().findViewById(R.id.cancel_view);

        rvWorkType = holder.getPop().findViewById(R.id.rv_work_type);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectResultClick.result(pickSelectEntityList, name);
                dissMiss();
            }
        });
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });

        workTypeAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        workTypeAdapter.setMultiSelect(true);
        workTypeAdapter.setDefItem(-1);
        rvWorkType.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvWorkType.setAdapter(workTypeAdapter);
        workTypeAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> pickSelectEntities, int position) {
                pickSelectEntityList = pickSelectEntities;
                if (pickSelectEntities == null || pickSelectEntities.size() == 0) {
                    name = "";
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (PickSelectEntity pickSelectEntity : pickSelectEntities) {
                        stringBuilder.append(pickSelectEntity.getProjectscale());
                        stringBuilder.append("、");
                    }
                    name = stringBuilder.substring(0, stringBuilder.length() - 1);
                }
            }
        });
    }

    public void setMostSelected(int num) {
        if (workTypeAdapter != null) {
            workTypeAdapter.setMost(num);
        }
    }

    public void setWorkTypeData(List<PickSelectEntity> data) {
        workTypeAdapter.setDatas(data);
        workTypeAdapter.setDefItem(-1);
    }

    public interface SelectResultClick {

        void result(List<PickSelectEntity> pickSelectEntities, String name);
    }

    private SelectResultClick selectResultClick;

    public void setSelectResultClick(SelectResultClick selectResultClick) {
        this.selectResultClick = selectResultClick;
    }
}
